<?php
/**
 * created by zzy
 * date: 2018/1/26 14:04
 */
namespace App\Eloquent\Ygt;

use Illuminate\Database\Eloquent\SoftDeletes;
class WarehouseLog extends DbEloquent{
    use SoftDeletes;
    protected $dates = ['deleted_at'];
    protected $table    = 'ygt_warehouse_log';
    public static function getSn($userId)
    {
        $ymd                = date('YmdHis',$_SERVER['REQUEST_TIME']);
        $sn                 = $ymd.$userId.mt_rand(1000,9999);
        return $sn;
    }
    public static function getTypeList($type=0)
    {
        $lists              = [
            ['id'=>0,'title'=>'异常数据','color'=>'#04C9B3'],
            ['id'=>1,'title'=>'成品入库','color'=>'#04C9B3'],
            ['id'=>2,'title'=>'出库单出库','color'=>'#FFB401']
        ];
        if($type==1){
            $result         = [];
            foreach($lists as $key=>$val)
            {
                $result[$val['id']] = $val['title'];
            }
        }else{
            $result             = $lists;
        }
        return $result;
    }
    public static function getWaterList($where,$limit='',$offset='',$orderby=['ygt_warehouse_log.id','desc'])
    {
        $fields                 = 'ygt_warehouse_log.*';
        $fields                 .= ',ygt_user.truename as operate_name';
        $fields                 .= ',ygt_warehouse.product_name';
        $join                   = [
            ['table'=>'ygt_warehouse','field_l'=>'ygt_warehouse.id','field_c'=>'=','field_r'=>'ygt_warehouse_log.warehouse_id'],
            ['table'=>'ygt_user', 'field_l'=>'ygt_user.id', 'field_c'=>'=', 'field_r'=>'ygt_warehouse_log.user_id'],
        ];
        $groupby                = '';
        $collection             = self::getList($where,$fields,$limit,$offset,$orderby,$groupby,$join);
        $typeList               = self::getTypeList();
        foreach($collection as $key=>$val)
        {
            $arr                = $val->toArray();
            $valType            = $arr['type'];
            $valCreatedAt       = $arr['created_at'];
            $typeName           = '异常数据';
            $typeColor          = '#04C9B3';
            foreach($typeList as $k=>$v)
            {
                if($v['id'] == $valType)
                {
                    $typeName       = $v['title'];
                    $typeColor      = $v['color'];
                }
            }
            $collection[$key]->type_name        = $typeName;
            $collection[$key]->type_color        = $typeColor;
            $createdAtYmd       = date('Y-m-d H:i:s',$valCreatedAt);
            $collection[$key]->created_at_ymd        = $createdAtYmd;
        }
        return $collection;
    }
}
